﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace Org.Kaka.CsmsUtil.Xml
{
    public class XmlHepler
    {
        private string xmlPuth;

        public string XmlPuth
        {
            get { return xmlPuth; }
            set { xmlPuth = value; }
        }
        /// <summary>
        /// 创建Xml文件
        /// </summary>
        /// <param name="xmlPath">创建文件路径</param>
        /// <param name="element"></param>
        public void CreateXmlNode(string xmlPath, string element)
        {
            //实例化Xml文档类
            XmlTextWriter xmlTextWriter = new XmlTextWriter(xmlPath, Encoding.UTF8);
            //创建Xml声明
            xmlTextWriter.WriteStartDocument();
            xmlTextWriter.WriteStartElement(element);
            xmlTextWriter.Flush();
            xmlTextWriter.Close();
        }
        public void CreateXmlNode(string element)
        {
            CreateXmlNode(GetMapPath(), element);
        }
        /// <summary>
        /// 新增Xml节点
        /// </summary>
        /// <param name="xmlPath"></param>
        /// <param name="singleNode"></param>
        /// <param name="node"></param>
        public void AddXmlElement(string xmlPath, string singleNode, XmlNode node)
        {
            //实例化Xml文档类
            XmlDocument xmlDocument = new XmlDocument();
            //加载Xml文件
            xmlDocument.Load(xmlPath);
            xmlDocument.SelectSingleNode(singleNode).AppendChild(node);
            xmlDocument.Save(xmlPath);
        }
        public void AddXmlElement(string singleNode, XmlNode node)
        {
            AddXmlElement(GetMapPath(), singleNode, node);
        }
        /// <summary>
        /// 新增Xml节点
        /// </summary>
        /// <param name="xmlPath"></param>
        /// <param name="singleNode"></param>
        /// <param name="node"></param>
        public void AddXmlElement(string xmlPath, string singleNode, string element, string attribute, string value)
        {
            //实例化Xml文档类
            XmlDocument xmlDocument = new XmlDocument();
            //加载Xml文件
            xmlDocument.Load(xmlPath);
            XmlNode xmlNode = xmlDocument.SelectSingleNode(singleNode);
            XmlElement xmlElement = xmlDocument.CreateElement(element); ;
            xmlElement.SetAttribute(attribute, value);
            xmlDocument.Save(xmlPath);
        }
        public void AddXmlElement(string singleNode, string element, string attribute, string value)
        {
            AddXmlElement(GetMapPath(), singleNode, element, attribute, value);
        }

        /// <summary>
        /// 读取XML文件的XmlDocument
        /// </summary>
        /// <param name="xmlPath">Xml文件路径</param>
        /// <returns></returns>
        public XmlDocument GetXmlDocument(string xmlPath)
        {
            try
            {
                //实例化Xml文档类
                XmlDocument xmlDocument = new XmlDocument();
                //加载Xml文件
                xmlDocument.Load(GetMapPath(xmlPath));
                return xmlDocument;
            }
            catch (System.Xml.XmlException exp)
            {
                throw exp;
            }
            catch (System.IO.IOException exp)
            {
                throw exp;
            }
            catch (ArgumentNullException exp)
            {
                throw exp;
            }
            catch (ArgumentException exp)
            {
                throw exp;
            }
            catch (Exception exp)
            {
                throw exp;
            }
        }
        public XmlDocument GetXmlDocument()
        {
            return GetXmlDocument(GetMapPath());
        }
        /// <summary>
        /// 读取XML文件的XmlNode
        /// </summary>
        /// <param name="xmlPath">Xml文件路径</param>
        /// <param name="singleNode">选择匹配XPath表达式的第一个XmlNode</param>
        /// <returns></returns>
        public XmlNode GetXmlNode(string xmlPath, string singleNode)
        {
            try
            {
                //实例化Xml文档类
                XmlDocument xmlDocument = new XmlDocument();
                //加载Xml文件
                xmlDocument.Load(GetMapPath(xmlPath));
                //获取文件内容
                return xmlDocument.SelectSingleNode(singleNode);
            }
            catch (System.Xml.XPath.XPathException exp)
            {
                throw exp;
            }
            catch (System.Xml.XmlException exp)
            {
                throw exp;
            }
            catch (System.IO.IOException exp)
            {
                throw exp;
            }
            catch (ArgumentNullException exp)
            {
                throw exp;
            }
            catch (ArgumentException exp)
            {
                throw exp;
            }
            catch (Exception exp)
            {
                throw exp;
            }
        }
        public XmlNode GetXmlNode(string singleNode)
        {
            return GetXmlNode(GetMapPath(), singleNode);
        }
        /// <summary>
        /// 读取XML文件的XmlElement
        /// </summary>
        /// <param name="xmlPath">Xml文件路径</param>
        /// <param name="singleNode">选择匹配XPath表达式的第一个XmlNode</param>
        /// <param name="xmlNodeString">节点名称</param>
        /// <returns></returns>
        public XmlElement GetXmlElement(string xmlPath, string singleNode, string xmlNodeString)
        {
            try
            {
                //实例化Xml文档类
                XmlDocument xmlDocument = new XmlDocument();
                //加载Xml文件
                xmlDocument.Load(GetMapPath(xmlPath));
                //获取文件内容
                XmlNode xmlNode = xmlDocument.SelectSingleNode(singleNode);
                //提取节点名称下的属性值
                return xmlNode[xmlNodeString];
            }
            catch (System.Xml.XPath.XPathException exp)
            {
                throw exp;
            }
            catch (System.Xml.XmlException exp)
            {
                throw exp;
            }
            catch (System.IO.IOException exp)
            {
                throw exp;
            }
            catch (ArgumentNullException exp)
            {
                throw exp;
            }
            catch (ArgumentException exp)
            {
                throw exp;
            }
            catch (Exception exp)
            {
                throw exp;
            }

        }
        public XmlElement GetXmlElement(string singleNode, string xmlNodeString)
        {
            return GetXmlElement(GetMapPath(), singleNode, xmlNodeString);
        }
        /// <summary>
        /// 读取XML文件的节点属性
        /// </summary>
        /// <param name="xmlPath">Xml文件路径</param>
        /// <param name="singleNode">选择匹配XPath表达式的第一个XmlNode</param>
        /// <param name="xmlNodeString">节点名称</param>
        /// <param name="variables">节点属性</param>
        /// <returns></returns>
        public string GetAttribute(string xmlPath, string singleNode, string xmlNodeString, string variables)
        {
            try
            {
                //实例化Xml文档类
                XmlDocument xmlDocument = new XmlDocument();
                //加载Xml文件
                xmlDocument.Load(GetMapPath(xmlPath));
                //获取文件内容
                XmlNode xmlNode = xmlDocument.SelectSingleNode(singleNode);
                //提取节点名称下的属性值
                return xmlNode[xmlNodeString].GetAttribute(variables);
            }
            catch (System.Xml.XPath.XPathException exp)
            {
                throw exp;
            }
            catch (System.Xml.XmlException exp)
            {
                throw exp;
            }
            catch (System.IO.IOException exp)
            {
                throw exp;
            }
            catch (ArgumentNullException exp)
            {
                throw exp;
            }
            catch (ArgumentException exp)
            {
                throw exp;
            }
            catch (Exception exp)
            {
                throw exp;
            }

        }
        public string GetAttribute(string singleNode, string xmlNodeString, string variables)
        {
            return GetAttribute(GetMapPath(), singleNode, xmlNodeString, variables);
        }
        /// <summary>
        /// 设置XML节点
        /// </summary>
        /// <param name="xmlNodeString">节点名称</param>
        /// <param name="value">节点值</param>
        /// <param name="discrption">节点说明</param>
        public void SetAttribute(string xmlPath, string singleNode, string xmlNodeString, string attribute, string value, string description)
        {
            try
            {
                XmlDocument xmlDocument = new XmlDocument();

                xmlDocument.Load(GetMapPath(xmlPath));
                //获取文件内容
                XmlNode xmlNode = xmlDocument.SelectSingleNode(singleNode);
                //设置指定XML节点Value值
                xmlNode[xmlNodeString].SetAttribute(attribute, value);
                xmlNode[xmlNodeString].SetAttribute("Description", description);
                //将设置后的XML节点保存
                xmlDocument.Save(GetMapPath(xmlPath));
            }
            catch (Exception exp)
            {
                throw new Exception(exp.Message);
            }
        }
        public void SetAttribute(string singleNode, string xmlNodeString, string attribute, string value, string description)
        {
            SetAttribute(GetMapPath(), singleNode, xmlNodeString, attribute, value, description);
        }
        /// <summary>
        /// 设置XML节点
        /// </summary>
        /// <param name="xmlPath">XML文件路径</param>
        /// <param name="singleNode">选择匹配XPath表达式的第一个XmlNode</param>
        /// <param name="xmlNodeString">指定节点</param>
        /// <param name="attribute">属性数组</param>
        /// <param name="value">值数组</param>
        public void SetAttribute(string xmlPath, string singleNode, string xmlNodeString, string[] attribute, string[] value)
        {
            try
            {
                XmlDocument xmlDocument = new XmlDocument();

                xmlDocument.Load(GetMapPath(xmlPath));
                //获取文件内容
                XmlNode xmlNode = xmlDocument.SelectSingleNode(singleNode);
                for (int i = 0; i < attribute.Length; i++)
                {
                    //设置指定XML节点Value值
                    xmlNode[xmlNodeString].SetAttribute(attribute[i], value[i]);
                }
                //将设置后的XML节点保存
                xmlDocument.Save(xmlPath);
            }
            catch (Exception exp)
            {
                throw new Exception(exp.Message);
            }
        }
        public void SetAttribute(string singleNode, string xmlNodeString, string[] attribute, string[] value)
        {
            SetAttribute(GetMapPath(), singleNode, xmlNodeString, attribute, value);
        }

        /// <summary>
        /// 获取XML物理路径
        /// </summary>
        /// <param name="xmlName"></param>
        /// <returns></returns>
        private string GetMapPath()
        {
            if (string.IsNullOrEmpty(xmlPuth))
                throw new Exception("XML路径为空！");
            return xmlPuth;
        }
        private string GetMapPath(string xmlPuth)
        {
            if (string.IsNullOrEmpty(xmlPuth))
                throw new Exception("XML路径为空！");
            return xmlPuth;
        }
    }
}
